package com.hjt.gulimall.auth.controller;

import com.hjt.common.constant.AuthConstant;
import com.hjt.common.utils.R;
import com.hjt.common.utils.RandomUtils;
import com.hjt.gulimall.auth.feign.MemberFeignService;
import com.hjt.gulimall.auth.feign.ThreePartyFeignService;
import com.hjt.gulimall.auth.service.RegisterService;
import com.hjt.gulimall.auth.vo.UserRegisterVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * @author:97007
 * @date:2021/9/28 12:30
 */
@Controller
public class RegisterController {

    @Autowired
    RegisterService registerService;

    @Autowired
    MemberFeignService memberFeignService;

    @ResponseBody
    @GetMapping("/sms/sendcode")
    public R sendCode(@RequestParam("email") String email) {
        if (StringUtils.hasLength(email)) {
            if (registerService.sendCode(email)) {
                return R.ok();
            }
            else {
                return R.error();
            }
        } else {
            return R.error();
        }
    }
    /**
     *
     * @param registerVo
     * @param result
     * @param model 模拟重定向携带数据，存放在了session中
     * @return
     */
    @PostMapping("/register")
    public String register(@Validated UserRegisterVo registerVo, BindingResult result, RedirectAttributes model) {
        // 校验出错，转发到注册页
        if (result.hasErrors()){
            Map<String, String> errorMap = new HashMap<>();
            for (FieldError fieldError : result.getFieldErrors()) {
                errorMap.put(fieldError.getField(), fieldError.getDefaultMessage());
            }
            model.addFlashAttribute("errors", errorMap);
            return "redirect:http://auth.gulimall.com/register.html";
        }
        // 校验验证码
        String code = registerVo.getCode();
        // 校验不通过
        if (!registerService.verifyCode(registerVo.getEmail(),code)) {
            Map<String, String> errorMap = new HashMap<>();
            errorMap.put("code","验证码错误");
            model.addFlashAttribute("errors", errorMap);
            return "redirect:http://auth.gulimall.com/register.html";
        } else {
            // 注册
            R registerResult = memberFeignService.register(registerVo);
            // 注册成功
            if (registerResult.getCode() == 0) {
                // 删除验证码
                registerService.deleteCode(registerVo.getEmail());
                return "redirect:http://auth.gulimall.com";
            } else {
                // 注册失败
                Map<String, String> errorMap = new HashMap<>();
                errorMap.put("msg",registerResult.getMessage());
                model.addFlashAttribute("errors",errorMap);
                return "redirect:http://auth.gulimall.com/register.html";
            }
        }
    }
}
